package com.huitone.gddw.service.impl.monitoring.sms;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.monitoring.sms.IAccessReportDao;
import com.huitone.gddw.service.dto.monitoring.sms.AccessReportACDTO;
import com.huitone.gddw.service.dto.monitoring.sms.AccessReportDTO;
import com.huitone.gddw.service.dto.monitoring.sms.AccessUpRepACDTO;
import com.huitone.gddw.service.dto.monitoring.sms.AccessUpRepDTO;
import com.huitone.gddw.service.monitoring.sms.IAccessReportService;
import com.huitone.gddw.utils.DateTimeUtils;

/**
 * 接入汇总监控 接口实现类
 * @author wuluodan
 *
 */
/**
 * @author wpn
 *
 */
/**
 * @author wpn
 *
 */
/**
 * @author wpn
 *
 */
@Service
public class AccessReportService implements IAccessReportService {
	private final static Logger LOGGER = Logger.getLogger(AccessReportService.class);
	@Autowired
	private IAccessReportDao accessReportDao;
	
	private static ThreadPoolExecutor   threadPool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MINUTES,   new LinkedBlockingQueue<Runnable>());
	
	@Override
	public AccessReportACDTO find(Map<String, Object> params) {
		List<AccessReportDTO> all = new ArrayList<AccessReportDTO>();
		List<AccessReportDTO> list = accessReportDao.find(params);
		AccessReportACDTO ar = accessReportDao.findCount(params);
		boolean isNow = this.hasToday(params);
		if (isNow) {
//			String today = DateTimeUtils.dateToStr(new Date(), "yyyy-MM-dd");
//			Map<String, Object> curDate = new HashMap<String, Object>();
//			params.put("today", today);
//			curDate.put("endDate", today);
			AccessReportDTO accessReportDTO = accessReportDao.selectSendSm(params);
			if (accessReportDTO != null) {
//				accessReportDTO.setUpTotal(accessReportDao.selectRecvSm(curDate).getUpTotal());
				all.add(accessReportDTO);
				ar.setSvrNodeCount(ar.getSvrNodeCount() + accessReportDTO.getSvrNodeNum());
				ar.setTotalCount(ar.getTotalCount() + accessReportDTO.getTotal());
//				ar.setPeakRateCount(ar.getPeakRateCount() > accessReportDTO.getPeakRate() 
//						? ar.getPeakRateCount() : accessReportDTO.getPeakRate());
				ar.setSendSucCount(ar.getSendSucCount() + accessReportDTO.getSendSuc());
				ar.setSendFailCount(ar.getSendFailCount() + accessReportDTO.getSendFail());
				ar.setSendSucRateCount(ar.getTotalCount() == 0 
						? "0%" : String.format("%.2f", (float)ar.getSendSucCount() / ar.getTotalCount() * 100) + "%");
				ar.setRecvSucCount(ar.getRecvSucCount() + accessReportDTO.getRecvSuc());
				ar.setRecvFailCount(ar.getRecvFailCount() + accessReportDTO.getRecvFail());
				ar.setRecvNoneCount(ar.getRecvNoneCount() + accessReportDTO.getRecvNone());
//				ar.setUpTotalCount(ar.getUpTotalCount() + accessReportDTO.getUpTotal());
			}
		}
		all.addAll(list);
		ar.setList(all);
		return ar;
	}

	@Override
	public AccessUpRepACDTO findUp(Map<String, Object> params) {
		List<AccessUpRepDTO> all = new ArrayList<AccessUpRepDTO>();
		List<AccessUpRepDTO> list = accessReportDao.findUp(params);
		AccessUpRepACDTO ar = accessReportDao.findCountUp(params);
		boolean isNow = this.hasToday(params);
		if (isNow) {
//			String today = DateTimeUtils.dateToStr(new Date(), "yyyy-MM-dd");
//			Map<String, Object> curDate = new HashMap<String, Object>();
//			curDate.put("startDate", today);
//			curDate.put("endDate", today);
			AccessUpRepDTO accessUpRepDTO = accessReportDao.selectRecvSm(params);
			if (accessUpRepDTO != null) {
				all.add(accessUpRepDTO);
				ar.setSvrNodeCount(ar.getSvrNodeCount() + accessUpRepDTO.getSvrNodeNum());
				ar.setUpTotalCount(ar.getUpTotalCount() + accessUpRepDTO.getUpTotal());
			}
		}
		all.addAll(list);
		ar.setList(all);
		return ar;
	}
	
	/**
	 * 判断是否查询当天
	 * @param params
	 * @return
	 */
	private boolean hasToday(Map<String, Object> params) {
		boolean isNow = true;
		Date now = new Date();
		if (params.get("startDate") != null && !"".equals(params.get("startDate").toString())) {
			Date startDate = DateTimeUtils.strToDate(params.get("startDate").toString() + " 00:00:00");
			if (startDate.after(now)) {
				isNow = false;
			}
		}
		if (isNow && params.get("endDate") != null && !"".equals(params.get("endDate").toString())) {
			Date endDate = DateTimeUtils.strToDate(params.get("endDate").toString() + " 23:59:59");
			if (endDate.before(now)) {
				isNow = false;
			}
		}
		return isNow;
	}
	
	
	/**获取三个指标
	 * 获取上5分钟的发送超时短信数量，上5分钟的报文发送成功数，上5分钟的报文发送成功数率
	 */
	public Map<String, Object> getNewIndicator(CountDownLatch countDownLatch ) {
		
		Map<String, Object> hash = new HashMap<String, Object>();
		threadPool.execute(new Task(countDownLatch,hash));
		
		return hash;
	}
	
	public class Task implements Runnable{
		CountDownLatch countDownLatch ;
		Map<String, Object> hash;
		Task(CountDownLatch countDownLatch,Map<String, Object> hash ){
			this.countDownLatch= countDownLatch;
			this.hash= hash;
		}
		@Override
		public void run() {
			long currentTimeMillis = System.currentTimeMillis(); 
			
			
			Integer sendTimeoutCount = accessReportDao.getSendTimeoutCount();
			Integer successfulCount = accessReportDao.getSuccessfulCount();
			Integer sendTotal = accessReportDao.getSendTotal();
			hash.put("sendTimeoutCount", sendTimeoutCount);
			hash.put("successfulCount", successfulCount);
			
			if(sendTotal != null && sendTotal != 0 ) {
				//计算成功率
				float sendRate = (float)successfulCount/sendTotal * 100;
				//保留两位小数
				DecimalFormat df = new DecimalFormat("#.00"); 
				hash.put("successfulRate", df.format(sendRate));
			}else {
				hash.put("successfulRate", 100);
			}
			countDownLatch.countDown();
			LOGGER.info("*获取三个指标线程任务耗时："+(System.currentTimeMillis() - currentTimeMillis) );
		}
	}

}
